import 'dart:math';

import 'package:flutter/material.dart';

class RoomList extends StatefulWidget {
  RoomList({super.key, required this.point, required this.building});

  // 参数个数有 2 个
  String point;
  String building;

  @override
  State<RoomList> createState() => _RoomListState();
}

class _RoomListState extends State<RoomList> {
  // 1. 定义初始数据
  Map data = {
    'point': '', // 小区信息
    'building': '', // 楼栋信息
    'rooms': [], // 随机产生的房间号
  };

  @override
  void initState() {
    super.initState();
    data['point'] = widget.point;
    data['building'] = widget.building;
    _makeData();
  }

  _makeData() {
//随机数
    final ran = Random();
    //房间数
    final count = ran.nextInt(10) + 1;
    //房间号码
    List _rooms = [];

    for (var i = 0; i < count; i++) {
      //房间的楼层+房间号码
      final floor = ran.nextInt(6) + 1;
      final room = ran.nextInt(6) + 1;
      //先判断房间号码是否已经存在
      //不存在在添加
      if (!_rooms.contains('${floor}0${room}')) {
        _rooms.add('${floor}0${room}');
      }
    }
    setState(() {
      data['rooms'] = _rooms;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: const Text('选择房间'),
        ),
        body: ListView.builder(
          itemBuilder: (BuildContext context, int index) {
            return GestureDetector(
              onTap: () {
                Navigator.pushNamed(context, '/house_form', arguments: {
                  'point': data['point'],
                  'building': data['building'],
                  'room': data['rooms'][index]
                });
              },
              child: Container(
                  color: Colors.white,
                  padding: const EdgeInsets.all(10),
                  child: Row(children: [
                    Expanded(
                        child: Text(
                            '${data['point']}${data['building']}${data['rooms'][index]}室')),
                    const Row(children: [
                      Icon(Icons.arrow_forward_ios,
                          size: 16, color: Colors.black),
                    ])
                  ])),
            );
          },
          itemCount: (data['rooms'] as List).length,
        ));
  }
}
